package com.maxlen.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.Query;
import com.maxlen.common.utils.R;
import com.maxlen.goods.dao.SpuImagesDao;
import com.maxlen.goods.entity.SpuImagesEntity;
import com.maxlen.goods.service.SpuImagesService;
import com.maxlen.goods.service.SpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("spuImagesService")
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesDao, SpuImagesEntity> implements SpuImagesService {

    @Lazy
    @Autowired
    private SpuInfoService spuInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuImagesEntity> page = this.page(
                new Query<SpuImagesEntity>().getPage(params),
                new QueryWrapper<SpuImagesEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(Long spuId, List<String> images) {
        if(!images.isEmpty()){
            List<SpuImagesEntity> spuImagesEntityList = images.stream().map(img -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                spuImagesEntity.setSpuId(spuId);
                spuImagesEntity.setImgUrl(img);
                return spuImagesEntity;
            }).collect(Collectors.toList());

            this.saveBatch(spuImagesEntityList);
        }
    }

    @Override
    public R getImageByCategoryIdList(List<Long> categoryIds) {
        if(categoryIds.isEmpty()){
            return R.ok().setData(null);
        }
        List<Long> spuIds = spuInfoService.getIdsByCategoryIds(categoryIds);
        LambdaQueryWrapper<SpuImagesEntity> select = new LambdaQueryWrapper<SpuImagesEntity>()
                .select(SpuImagesEntity::getImgUrl);
        List<String> images = new ArrayList<>();
        if(spuIds.size() > 0) {
            select.in(SpuImagesEntity::getSpuId, spuIds);
            List<SpuImagesEntity> spuImagesEntityList = baseMapper.selectList(select);
            images = spuImagesEntityList.stream().map(SpuImagesEntity::getImgUrl).collect(Collectors.toList());
        }

        return R.ok().setData(images);
    }

    @Override
    public R getImageBySpuIdList(List<Long> spuIds) {
        if(spuIds.isEmpty()){
            return R.ok().setData(null);
        }
        List<SpuImagesEntity> spuImagesEntityList = baseMapper.selectList(new LambdaQueryWrapper<SpuImagesEntity>()
                .select(SpuImagesEntity::getImgUrl)
                .in(SpuImagesEntity::getSpuId, spuIds));
        List<String> images = spuImagesEntityList.stream().map(SpuImagesEntity::getImgUrl).collect(Collectors.toList());
        return R.ok().setData(images);
    }

}
